<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./font/iconfont.css">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        header {
            font-size: 20px;
            color: white;
            align-items: center;
            height: 50px;
            background-color: rgb(67, 165, 222);
            display: flex;
            justify-content: space-between;
            padding: 0 10px;
        }

        header .iconfont {
            margin: 0 10px;
        }

        main {
            display: flex;
        }

        main .left {
            padding: 40px auto;
            font-size: 16px;
            color: rgb(232, 232, 232);
            text-align: center;
            min-width: 150px;
            min-height: 700px;
            line-height: 60px;
            background-color: rgb(67, 165, 222);
        }

        main .left div.active {
            background-color: rgb(105, 141, 176);
        }

        main .left .iconfont {
            margin-right: 10px;
        }

        main .right {
            flex: 1;
            padding: 20px;
        }

        main .right .shang input {
            height: 30px;
            width: 180px;
            border-radius: 15px;
            outline: none;
            border: 1px solid rgb(202, 202, 202);
            padding-left: 20px;
        }

        main .right .shang button {
            margin-left: 10px;
            width: 70px;
            background-color: rgb(118, 194, 118);
            color: white;
            outline: none;
            height: 30px;
            border-radius: 10px;
            border: 1px solid transparent;
        }

        main .right button {
            margin: 10px;
            width: 90px;
            background-color: rgb(118, 194, 118);
            color: white;
            outline: none;
            height: 30px;
            border-radius: 5px;
            border: 1px solid transparent;
        }

        main .right table {
            /* 想要给表格tr添加边框线，需要给table写如下代码，才能让边框显示出来 */
            border-collapse: collapse;
            margin: 20px 0;
            width: 100%;
            min-width: 700px;
            text-align: center;
        }

        main .right table tbody tr {
            line-height: 41px;
            height: 41px;
            border-top: 1px solid rgb(221, 221, 221);
            min-width: 600px;
        }

        main .right table tbody tr:hover {
            background-color: rgba(240, 240, 240, 0.841);
        }

        main .right table tr td:nth-child(6) {
            background-color: rgb(118, 194, 118);
            display: inline-block;
            height: 30px;
            width: 60px;
            color: white;
            outline: none;
            border-radius: 8px;
            text-align: center;
            line-height: 30px;
        }

        main .right table tr td:nth-child(6).gc {
            background-color: rgb(245, 66, 66);

        }

        main .right table tr td:first-child,
        main .right table tr th:first-child {
            text-align: left;
        }

        main .right table tbody .trr {
            text-align: center;
            color: red;
            line-height: 41px;
            height: 41px;
            border-top: 1px solid rgb(221, 221, 221);
        }

        main .right table tbody .trr .tdd {
            text-align: center;
            height: 43px;
        }

        main .right table tr td .iconfont {
            padding-right: 10px;
        }

        main .right .yeniu {
            margin: 0 auto;
            width: 200px;
        }

        main .right .yeniu button {
            width: 80px;
            height: 30px;
        }
    </style>
</head>

<body>
    <header>
        <span>9644云专题</span>
        <div><span class="iconfont">&#xe649;</span><span class="username">猫狸</span>（9644云专题）</div>
    </header>
    <main>
        <div class="left">
            <div><span class="iconfont">&#xe61e;</span>首页</div>
            <div><span class="iconfont">&#xe786;</span>专题</div>
            <div class="active"><span class="iconfont">&#xe649;</span>用户</div>
            <div><span class="iconfont">&#xe617;</span>设置</div>
            <div><span class="iconfont">&#xe609;</span>日志</div>
        </div>
        <div class="right">
            <div class="shang">
                <input type="text" placeholder="搜索用户">
                <button>搜索</button>
            </div>
            <button class="zhong">+ 添加用户</button>
            <table>
                <thead>
                    <tr>
                        <th>邮箱（账户）</th>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>所属组</th>
                        <th>管理员</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>邮箱（账户）</td>
                        <td>姓名</td>
                        <td>电话</td>
                        <td>所属组</td>
                        <td>管理员</td>
                        <td>状态</td>
                        <td><span class="iconfont">&#xe616;</span><span class="iconfont" style=" padding-right: 0;">&#xe627;</span></td>
                    </tr> -->
                </tbody>
            </table>
            <div class="yeniu">
                <button>上一页</button><button>下一页</button>
            </div>
        </div>
    </main>
</body>

</html>
<script src="./axios.min.js"></script>
<!-- <script src="./mock.js"></script> -->
<!-- <script src="./mock-data.js"></script> -->
<!-- <script src="./promise-ajax.js"></script> -->
<script>

//   全局设置axios拦截器
    axios.interceptors.request.use(
        (config)=>{
            config.headers['Authorization']='Bearer'+''+localStorage.getItem('token')
            return config
        },
        (error)=>{
            return Promise.reject(error)
        }
    )
//   全局设置axios响应拦截器
    axios.interceptors.response.use(
        (res)=>{
            return res
        },
        (error)=>{
            if(error.response.status == 401){
                // alert('登录过期，请重新登录')
                location.href = './main.html'
                localStorage.removeItem('token')
            }else if (error.response.status == 404){
                alert('访问路径有误')
            }else if (error.response.status == 500){
                alert('服务器内部错误')
            }
            return Promise.reject(error)
        }
    )





















    // 页面数据渲染=============================
    function render(data) {
        var str = ''
        var str = data.map((item, index) => {
            return `
            <tr>
                <td>${item.email}</td>
                <td>${item.name}</td>
                <td>${item.phone}</td>
                <td>${item.isgroup}</td>
                <td>${item.admin ? '是' : '否'}</td>
                <td class ="${item.status ? '' : 'gc'}">${item.status ? '正常' : '异常'}</td>
                <td><span class="iconfont icon_write" data_id="">&#xe616;</span><span class="iconfont icon_del" data-phone="${item.phone}" style=" padding-right: 0;">&#xe627;</span></td>
            </tr>
            `
        }).join('')

        // str如果未查询到数据则是一个空的字符串，然而我们可以给它一个if
        if (str == '') {
            str = '<tr class = "trr" ><td class="tdd" colspan="7">未查询到数据!</td></tr>'
        }
        document.querySelector('tbody').innerHTML = str

    }
    // 获取搜索按钮和输入框=========================================
    let inputEle = document.querySelector('.shang>input')
    let buttonbtn = document.querySelector('.shang>button')


    let buttons = document.querySelectorAll('.yeniu>button')
    var page =1
    buttons[1].onclick = () => {
        page++
        axios.get('/user/list', { params:{page: page, count: 2} }).then((res) => {
            render(res.data.list)
        })
    }
    buttons[0].onclick = () => {
        axios.get('/user/list', { params:{page: page, count: 2} }).then((res) => {
            if(page >1){
                page--
            }else{
                page =1
            }

            render(res.data.list)
        })
    }





    // var page = 1
    // document.querySelectorAll('.yeniu>button')[0].onclick = () => {
    //     page++
    //     axios.get('/list', { page: page, count: 2 }).then((data) => {
    //         console.log('我被点击了');
    //         render(res.data.list)
    //     })
    // }









    // 第一步：：获取localStorage中的存储用户信息===因为原本字符串，用需要转成对象
    var userinfo = JSON.parse(localStorage.getItem('userinfo'))
    // 第二步：：渲染用户信息到首页=====
    console.log(userinfo);
    document.querySelector('.username').innerHTML = userinfo.name;

    // 给删除添加绑定事件
    var tbodys = document.querySelector('tbody')
    tbodys.onclick = (e) => {
        // if(e.target.tagName == 'SPAN' && e.target.className=='iconfont icon_del'){
        if (e.target.tagName == 'SPAN' && e.target.classList.contains('icon_del')) {
            // console.log(e.target.dataset.phone);
            axios.post('/user/delete', { phone: e.target.dataset.phone }).then((res) => {
                console.log(res.data);
                // 两种办法：
                // 方案一：本地删除这个dom节点（前端，）
                // e.target.parentNode.remove()
                // 方案二：让删除接口，返回删除后的数据，本地调用render渲染（服务器该做的）
                render(res.data.list)


            })

        }
    }






    // 点击搜索按钮，发起ajax请求，传参给服务器，服务器返回的搜索结果
    buttonbtn.onclick = () => {
        axios.get('/user/search', {
            params: {
                searchkey: inputEle.value
            }
        }).then((res) => {
            console.log(res.data);
            render(res.data.list)
        })
    }



    // axios从axios.min.js文件暴露，用来发送ajax请求
    axios.get('/user/list', { params: { page: 1, count: 2 },headers:{} }).then((res) => {
        render(res.data.list)//这里想要rendere方法调用后端数据渲染，用的map，所以必须用数组，而res.data不是数组，所以得加list，list是从服务端传送的数组列表
        console.log(res.data);
    })
    axios.post('/user/login', { phone: '13345678833', pass: '1234' }).then((res) => {
        console.log(res.data);
    })

</script>